<?php
if(!empty($_GET['editpref']))
{
  $details = decrypt($_GET['editpref'],'1b2u3z4z5r6');
  $details = explode('~',$details);
  $user_login = $details['0'];
  $city_name = $details['1'];
  $user_id = $details['2'];
  
  $link = 'http://buzzr.in/dailydeals/'.strtolower($city_name);
  
  wp_set_current_user($user_id, $user_login);
  wp_set_auth_cookie($user_id);
  do_action('wp_login', $user_login);
  wp_redirect($link);
  exit();  
}


if(!empty($_GET['daily']))
{
  require_once(ABSPATH . WPINC . '/registration.php');
  $decrypt = decrypt($_GET['daily'],'1b2u3z4z5r6');
  $decrypt = explode('~',$decrypt);
  $email = explode('@',$decrypt['0']);
  if(!email_exists($decrypt['0']))
  {   
    session_start();
    $_SESSION['daily_signup'] = 1;
    $_SESSION['daily_city'] = $decrypt['1'];
    $user_login = $email['0'];
    if(username_exists( $user_login ))
    {
      $user_login = $user_login.'1';
    }
    $random_password = wp_generate_password( 12, false );
    $user_id = wp_create_user($user_login, $random_password, $decrypt['0']);
    update_user_meta($user_id,'first_name',$user_login);
    update_user_meta($user_id,'last_name',$user_login);

    $to = $decrypt['0'];
    $city_id = $decrypt['1'];
	$source = $decrypt['2'];
	$medium = $decrypt['3'];
	$campaign = $decrypt['4'];
    
    $city_pod = new Pod('city',$city_id);
    $city_name = $city_pod->get_field('name');
    
    include_once(ABSPATH.'/wp-content/plugins/buzzr_newsletter/ses.php');
    $con=new SimpleEmailService("AKIAJEB25SWREHIRO2YA","Ota+tmICcb0aX+438v1fjmNizdCzlA/fzYr+G8cy");
    $con->listVerifiedEmailAddresses();
    
    $m = new SimpleEmailServiceMessage();
    $m->addTo($to);
    $m->setFrom('Buzzr.in<info@enthrallmedia.com>');
    $m->setSubject('Buzzr.in: Daily Newsletter Signup Confirmed');
    $m->setReturnPath('kumaril@enthrallmedia.com');
    
    //$sub = "Buzzr.in: Daily Newsletter Signup Confirmed";
        
    $msg = '<div id="body" style="width: 500px; border: solid; border-color: #FFFFFF; background-color: #FFFFFF;">
    <div id="logo" style="text-align: left">
    	<img style="margin-left: 178px; margin-top: 10px;" height="40" src="http://buzzr.in/wordpress/wp-content/uploads/2011/01/buzzrin-logo.png" />
    </div>
    <div id="content" style="width: auto; margin: 20px; padding: 5px; background-color: #D0F0FB; font-size: 14px; color:#1BA3DF;">
    Hello,<br /><br />
    Congratulations your daily deal newsletter signup for '.$city_name.' has been confirmed.<br /><br /> 
    <b>Your Buzzr Account details:</b><br />
    <b>Username: </b>'.$to.'<br />
    <b>Password: </b>'.$random_password.'<br /><br />
    <h4 style="display: inline;"><a title="Click here to manage your preferences" target="_blank" href="http://buzzr.in/editprofile">You can edit your preferences and change your password from your user profile</a></h4><br /><br />
    Thank You!
    
    <font><i><br /><br />This email was sent to through Buzzr.in<br />
    If this message does not apply to you please ignore the email Thank You! 
    </i></font>
    </div>
    
    </div>';
    
    $m->setMessageFromString('',$msg);
    $con->sendEmail($m);
    
    //$headers = array("From: Buzzr.in <info@enthrallmedia.com>","Content-Type: text/html");
    //$h = implode("\r\n",$headers) . "\r\n";
    //wp_mail($to, $sub, $msg, $h);
   
    $link = 'http://buzzr.in/dailydeals/'.strtolower($city_name).'/?signup=1';
	if(!empty($source)){
		$link = 'http://buzzr.in/dailydeals/'.strtolower($city_name).'/?signup=1&utm_source='.$source.'&utm_medium='.$medium.'&utm_campaign='.$campaign;
	}
    
    wp_set_current_user($user_id, $user_login);
    wp_set_auth_cookie($user_id);
    do_action('wp_login', $user_login);
    wp_redirect($link);
    exit();
  }
  else{
    $user_data = get_user_by('email',$decrypt['0']);
    $city_id = $decrypt['1'];
    $city_pod = new Pod('city',$city_id);
    $city_name = $city_pod->get_field('name');
    $link = 'http://buzzr.in/dailydeals/'.strtolower($city_name);
    wp_set_current_user($user_data->ID, $user_data->user_login);
    wp_set_auth_cookie($user_data->ID);
    do_action('wp_login', $user_data->user_login);
    wp_redirect($link);
    exit();
  }
}
?>

<?php
if(!empty($_GET['ref']))
{
  $visitors_email = $_GET['visitors_email'];
  $ref = $_GET['ref'];
  //$s = $_GET['s'];
  
  if(!isset($_COOKIE['buzzr_refer']))
  {
    setcookie("buzzr_refer", $ref, time()+2592000); //Cookie valid for a month
  }
   /*
    $sendto = encrypt($_SERVER['REQUEST_URI'],'1b2u3z4z5r6');
    $link = get_bloginfo('wpurl')."/wp-content/plugins/buzzr_invite/invite.php?sendto=".$sendto;
    header("Location: $link");
   */
}
?>
<?php
if(is_pod_page('dailydeals') || is_pod_page('localoffers'))
{
$last_var = pods_url_variable('last'); 

  if(is_user_logged_in()){
    $users_id = get_users_pod_id();
    $users = new Pod('users',$users_id);
    $users->page = 1;
    $users->search = false;
    $daily_city = $users->get_field('city.name');
    if(!empty($daily_city)){
      $daily_url = get_bloginfo('url').'/'.$last_var.'/'.strtolower($daily_city);
      wp_redirect( $daily_url ); exit;
    }
    elseif(isset($_COOKIE['buzzr_daily_city']))
    {
     $daily_city = $_COOKIE['buzzr_daily_city'];
     $daily_url = get_bloginfo('url').'/'.$last_var.'/'.$daily_city;
     wp_redirect( $daily_url ); exit;
    }
    else
    {
      $daily_url = get_bloginfo('url').'/'.$last_var.'/mumbai';
      wp_redirect( $daily_url ); exit;
    }
  }
  elseif(isset($_COOKIE['buzzr_daily_city']))
  {
   $daily_city = $_COOKIE['buzzr_daily_city'];
   $daily_url = get_bloginfo('url').'/'.$last_var.'/'.$daily_city;
   wp_redirect( $daily_url ); exit;
  }
}
?>
<?php //thematic_create_doctype(); echo " "; //language_attributes(); echo ">\n";?>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<?php if(is_pod_page('dailydeals')){
if(isset($_COOKIE['buzzr_daily_city']))
  {
   $daily_city = $_COOKIE['buzzr_daily_city'];
   $daily_url = get_bloginfo('url').'/dailydeals/'.$daily_city;
   wp_redirect( $daily_url ); exit;
  }
}
?>

<link rel="icon" type="image/png" href="<?php echo get_bloginfo('wpurl'); ?>/wp-content/uploads/2010/08/favicon.ico">
<?php 
global $pods;
if(isset($pods->data['meta_title'])){ ?>
<title><?php echo ($pods->data['meta_title'].' | '.get_bloginfo('name')); ?></title>
<?php
} else {
// normal title code here
thematic_doctitle();
}
thematic_create_contenttype();
thematic_show_description();
//thematic_show_robots();
thematic_canonical_url();
thematic_create_stylesheet();
thematic_show_rss();
thematic_show_commentsrss();
thematic_show_pingback();
thematic_show_commentreply();
wp_head();
?>

<link rel="stylesheet" href="<?php echo get_bloginfo('wpurl'); ?>/wp-content/pfiles/jqueryui/jquery-ui-1.8.6.custom.css" type="text/css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js" ></script>

<?php
global $current_user;
get_currentuserinfo();
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);

if ($user_role != 'administrator' && $user_role != 'moderator')
{
?>
<!-- Start of Adohana Tracking Code -->
<script type ="text/javascript" src="http://bsrv.adohana.com/ohana/adohana.js?usr=b6f45651887c51850f8de7a031a17fa5"></script>
<!-- End of Adohana Tracking Code -->
<?php
}
?>


<?php
if(!is_user_logged_in()){
  if(!is_pod_page('dailydeals') && !is_pod_page('dailydeals/*')){
?>
<script type="text/javascript">

function closeIframe()
{
    jQuery('#externalSite').dialog('close');
    return false;
}
    
//To display a single URL, just remove all but the first entry below:
var popurls=new Array()
//popurl="http://buzzr.in/wordpress/invite-popup2.php"
popurl="http://buzzr.in/wordpress/wp-login.php?action=register"

function openpopup(popurl){
var horizontalPadding = 30;
var verticalPadding = 30;
photoDialog = jQuery('<iframe id="externalSite" src="' + popurl + '" />').dialog({
    title: 'Register at Buzzr.in!',
    autoOpen: true,
    width: 430,
    height: 720,
    minWidth: 400,
    minHeight: 400, 
    modal: true,
    resizable: false,
    autoResize: true,
    draggable: false,
    close: function(event, ui){ 
        location.reload(true); 
    }
}).width(430 - horizontalPadding).height(720 - verticalPadding); 
}


function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if cookie exists
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1)
         end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function loadornot(){
if (get_cookie('buzzr_login')==''){
delay = 15;
timer = setTimeout("openpopup(popurl)", delay*1000);
var date = new Date();
date.setTime(date.getTime()+(90*24*60*60*1000));
document.cookie="buzzr_login=yes;expires="+date.toGMTString()
}
}

loadornot()
</script>
<?php
  }
}
//start custom stylesheet embed
// Get the last parameter from the URI (which happens to be the ID)
$iscampaignpage=pods_url_variable(-2);
if ($iscampaignpage == 'deals'){
$id = pods_url_variable('last');
$Record = new Pod('deal', $id);
$theme=$Record->get_field('theme.name');
//end custom stylesheet embed
}
else
{
$theme='Default';
}
if ($theme!=='Default'){
?>
<link rel="stylesheet" type="text/css" href="<?php echo get_bloginfo('stylesheet_url'); ?>" />
<?php
}

if(is_page('home'))
{
?>
<?php
/*
<!--
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js" ></script>
<script type="text/javascript" src=" //echo get_bloginfo('wpurl'); /wp-content/easyslider1.7/js/easySlider1.7.js"></script>
-->
*/
?>
<!--slider script-->
<script type="text/javascript">
jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
	$(document).ready(function(){
		$("#featured").tabs({ fx: { opacity: 'toggle' }}).tabs("rotate", 5000, true);
	});
});
</script>
<!--end slider script-->
<?php
}
?>

<!--<meta property="fb:admins" content="500296642" />-->
</head>
<body class="<?php thematic_body_class() ?>">

<?php thematic_before(); ?>

<div id="wrapper" class="hfeed">
<!-- <div style="margin: 0pt auto; width: 900px; text-align: center; font-size: 12px; height: 15px; padding: 2px; line-height: 15px; background: none repeat scroll 0% 0% LightCoral; color: black;">We're experiencing heavy traffic which is causing slow load times, please bear with us while we sort this out.</div> -->
<?php thematic_aboveheader(); ?>   

<div id="header">

<?php thematic_header() ?>        

</div><!-- #header-->
    
<?php thematic_belowheader(); ?>

<?php if(!is_pod_page('deals/*') && !is_pod_page('dailydeals') && !is_pod_page('dailydeals2') && !is_pod_page('dailydeals/*') && !is_pod_page('dailydeals4')){ ?>
<div class="search_wrap_nav">
<script type="text/javascript">
jQuery(document).ready(function($){
    $(".searchbar").focus(function (){
      var value = $(".searchbar").val();
      if(value == 'Enter Brand, Product or Keyword'){
      $(".searchbar").val('');
      $('.searchbar').css({'color': 'black'});
      }
    });

    $('.searchbar').blur(function() {
      var value = $(".searchbar").val();
      if(value == ''){
      $('.searchbar').css({'color': 'grey'});
      $(".searchbar").val('Enter Brand, Product or Keyword');
      }
    });
    
    $("#searchform").submit(function(){
      var value = $(".searchbar").val();
      if(value == 'Enter Brand, Product or Keyword' || value == ''){
      alert('Please Enter a Search Term');
      return false;
      }
    });
});
</script>
<form action="http://buzzr.in/search/" id="searchform" method="get">
<span class="search_text">Find Best Deals for </span><input type="text" class="searchbar" value="Enter Brand, Product or Keyword" id="q" name="q">
<input type="submit" class="button" value="Search" id="searchsubmit">
</form>
</div>
<?php }
else{
echo '<div class="searchbar"></div>';
} ?>


<?php
if(is_front_page())
{
  echo '<style>#footer{ width: 900px; }</style><div id="main" style="background: none;padding: 0px !important;">';
}
else
{
  echo '<div id="main">';
}
?>
